package com.qf.edu.controller;

import com.qf.edu.entity.Drugs;
import com.qf.edu.entity.Drugsgrant;
import com.qf.edu.service.DrugsGtService;
import com.qf.edu.service.impl.DrugsGtServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:发放药品控制台
 * @datatime: 2022/5/29 12:20
 */
@WebServlet("/drugsgrantCom")
public class DrugsgrantCom extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String idNumber = req.getParameter("idNumber");
        String name = req.getParameter("name");
        String drugName = req.getParameter("drugName");
        BigDecimal price = new BigDecimal(req.getParameter("price"));
        //Integer会自动装箱
        Integer changeNumber = Integer.parseInt(req.getParameter("changeNumber"));
        BigDecimal totalPrice = new BigDecimal(req.getParameter("totalPrice"));
        System.out.println("药品发放:");
        System.out.println(idNumber);
        System.out.println(name);
        System.out.println(drugName);
        System.out.println(price);
        System.out.println(changeNumber);
        System.out.println(totalPrice);

        Drugsgrant drugsgrant = new Drugsgrant();
        drugsgrant.setIdNumber(idNumber);
        drugsgrant.setName(name);
        drugsgrant.setDrugsname(drugName);
        drugsgrant.setPrice(price);
        drugsgrant.setNumber(changeNumber);
        drugsgrant.setTotal(totalPrice);

        DrugsGtService service = new DrugsGtServiceImpl();
        String s = service.drugsGtS(drugsgrant);
        int drugskp = service.drugskp(drugsgrant);
        int i = service.drugsGet(drugsgrant);
        if (drugskp == 1){
            System.out.println(s);
            resp.getWriter().println(s + "<br>" + "药品库存量更新通过,现价格" + price + "元的" + drugName + "药品库存量为：" + i + "<br>");
        } else {
            System.out.println("药品库存量更新失败");
            resp.getWriter().println("药品库存量更新失败<br>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
